package dca.sistemascontrole.view.configuracao;

import dca.sistemascontrole.malhadecontrole.MalhaFechada;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class PanelControlador extends javax.swing.JPanel {

    private int tipoControlador;
    private final int ALTERADO_KI = 1;
    private final int ALTERADO_TI = 2;
    private final int ALTERADO_KD = 3;
    private final int ALTERADO_TD = 4;
    private final int ALTERADO_NULL = -1;
    private int variaelDerivativa = ALTERADO_NULL;
    private int variaelIntegrativa = ALTERADO_NULL;
    private ConfiguracaoProntaListener configuracaoProntaListener;

    public PanelControlador() {
        initComponents();

        desativarTxts();

        adicionarDocumentListener();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel6 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtKP = new javax.swing.JTextField();
        txtKI = new javax.swing.JTextField();
        txtKD = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtTI = new javax.swing.JTextField();
        txtTD = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        radioP = new javax.swing.JRadioButton();
        radioPI = new javax.swing.JRadioButton();
        radioPD = new javax.swing.JRadioButton();
        radioPID = new javax.swing.JRadioButton();
        radioPI_D = new javax.swing.JRadioButton();

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel6.setBackground(new java.awt.Color(255, 255, 255));
        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Constantes"));

        jLabel2.setText("Kp:");

        jLabel3.setText("Ki:");

        jLabel4.setText("Kd:");

        txtKP.setText("0");

        jLabel5.setText("Ti:");

        jLabel6.setText("Td:");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtKD, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtKP, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtKI, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel6Layout.createSequentialGroup()
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtTD, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel6Layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtTI, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtKP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtKI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtKD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtTI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(txtTD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Tipo de Controle"));

        radioP.setBackground(new java.awt.Color(255, 255, 255));
        radioP.setText("P");
        radioP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioPActionPerformed(evt);
            }
        });

        radioPI.setBackground(new java.awt.Color(255, 255, 255));
        radioPI.setText("PI");
        radioPI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioPIActionPerformed(evt);
            }
        });

        radioPD.setBackground(new java.awt.Color(255, 255, 255));
        radioPD.setText("PD");
        radioPD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioPDActionPerformed(evt);
            }
        });

        radioPID.setBackground(new java.awt.Color(255, 255, 255));
        radioPID.setText("PID");
        radioPID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioPIDActionPerformed(evt);
            }
        });

        radioPI_D.setBackground(new java.awt.Color(255, 255, 255));
        radioPI_D.setText("PI-D");
        radioPI_D.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioPI_DActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(radioP)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(radioPI)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(radioPD)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(radioPID)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(radioPI_D)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radioP)
                    .addComponent(radioPI)
                    .addComponent(radioPD)
                    .addComponent(radioPID)
                    .addComponent(radioPI_D))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void adicionarDocumentListener() {
        txtKP.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                kpMudou();
            }

            public void removeUpdate(DocumentEvent e) {
                kpMudou();
            }

            public void changedUpdate(DocumentEvent e) {
                kpMudou();
            }
        });
        txtKD.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                kdMudou();
            }

            public void removeUpdate(DocumentEvent e) {
                kdMudou();
            }

            public void changedUpdate(DocumentEvent e) {
                kdMudou();
            }
        });
        txtTD.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                tdMudou();
            }

            public void removeUpdate(DocumentEvent e) {
                tdMudou();
            }

            public void changedUpdate(DocumentEvent e) {
                tdMudou();
            }
        });
        txtKI.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                kiMudou();
            }

            public void removeUpdate(DocumentEvent e) {
                kiMudou();
            }

            public void changedUpdate(DocumentEvent e) {
                kiMudou();
            }
        });
        txtTI.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent e) {
                tiMudou();
            }

            public void removeUpdate(DocumentEvent e) {
                tiMudou();
            }

            public void changedUpdate(DocumentEvent e) {
                tiMudou();
            }
        });
    }

    private void atualizarConstantes() {
        float kp = 0;
        if (txtKP.hasFocus()) {
            try {
                kp = Float.parseFloat(txtKP.getText());

                try {
                    if (variaelIntegrativa == ALTERADO_KI) {
                        float ki = Float.parseFloat(txtKI.getText());
                        txtTI.setText(String.valueOf(kp / ki));
                    } else if (variaelIntegrativa == ALTERADO_TI) {
                        float ti = Float.parseFloat(txtTI.getText());
                        txtKI.setText(String.valueOf(kp / ti));
                    }
                } catch (NumberFormatException ex) {
                } catch (IllegalStateException ex) {
                }

                try {
                    if (variaelDerivativa == ALTERADO_KD) {
                        float kd = Float.parseFloat(txtKD.getText());
                        txtTD.setText(String.valueOf(kd / kp));
                    } else if (variaelDerivativa == ALTERADO_TD) {
                        float td = Float.parseFloat(txtTD.getText());
                        txtKD.setText(String.valueOf(td * kp));
                    }
                } catch (NumberFormatException ex) {
                } catch (IllegalStateException ex) {
                }
            } catch (NumberFormatException ex) {
            } catch (IllegalStateException ex) {
            }
        }
    }

    private void kpMudou() {
        atualizarConstantes();
    }

    private void kiMudou() {
        try {
            if (txtKI.hasFocus()) {
                float kp = Float.parseFloat(txtKP.getText());
                float ki = Float.parseFloat(txtKI.getText());

                txtTI.setText(String.valueOf(kp / ki));
                variaelIntegrativa = ALTERADO_KI;
            }
        } catch (NumberFormatException ex) {
        } catch (IllegalStateException ex) {
        }
    }

    private void tiMudou() {
        try {
            if (txtTI.hasFocus()) {
                float kp = Float.parseFloat(txtKP.getText());
                float ci = Float.parseFloat(txtTI.getText());

                txtKI.setText(String.valueOf(kp / ci));
                variaelIntegrativa = ALTERADO_TI;
            }
        } catch (NumberFormatException ex) {
        } catch (IllegalStateException ex) {
        }
    }

    private void kdMudou() {
        try {
            if (txtKD.hasFocus()) {
                float kp = Float.parseFloat(txtKP.getText());
                float kd = Float.parseFloat(txtKD.getText());

                txtTD.setText(String.valueOf(kd / kp));
                variaelDerivativa = ALTERADO_KD;
            }
        } catch (NumberFormatException ex) {
        } catch (IllegalStateException ex) {
        }
    }

    private void tdMudou() {
        try {
            if (txtTD.hasFocus()) {
                float kp = Float.parseFloat(txtKP.getText());
                float cd = Float.parseFloat(txtTD.getText());

                txtKD.setText(String.valueOf(cd * kp));
                variaelDerivativa = ALTERADO_TD;
            }
        } catch (NumberFormatException ex) {
        } catch (IllegalStateException ex) {
        }
    }

    public void desativar() {
        radioP.setEnabled(false);
        radioPD.setEnabled(false);
        radioPI.setEnabled(false);
        radioPID.setEnabled(false);
        radioPI_D.setEnabled(false);

        desativarTxts();
    }

    public void ativar() {
        radioP.setEnabled(true);
        radioPD.setEnabled(true);
        radioPI.setEnabled(true);
        radioPID.setEnabled(true);
        radioPI_D.setEnabled(true);

        ativarTxts(tipoControlador);
    }

    private void desativarTxts() {
        txtKD.setEnabled(false);
        txtKI.setEnabled(false);
        txtKP.setEnabled(false);
        txtTD.setEnabled(false);
        txtTI.setEnabled(false);
    }

    public String getKP() {
        return txtKP.getText().trim().replace(",", ".");
    }

    public String getKI() {
        return txtKI.getText().trim().replace(",", ".");
    }

    public String getKD() {
        return txtKD.getText().trim().replace(",", ".");
    }

    public String getTD() {
        return txtTD.getText().trim().replace(",", ".");
    }

    public String getTI() {
        return txtTI.getText().trim().replace(",", ".");
    }

    public int getTipoControlador() {
        return tipoControlador;
    }

    public void setConfiguracaoProntaListener(ConfiguracaoProntaListener configuracaoProntaListener) {
        this.configuracaoProntaListener = configuracaoProntaListener;
    }

    private void fireConfiguracaoStatusAtualizado() {
        configuracaoProntaListener.configuracaoStatusAtualizado(this);
    }

    public void ativarTxts(int tipo) {
        switch (tipo) {
            case MalhaFechada.P:
                txtKP.setEnabled(true);
                txtKD.setEnabled(false);
                txtTD.setEnabled(false);
                txtKI.setEnabled(false);
                txtTI.setEnabled(false);
                fireConfiguracaoStatusAtualizado();
                break;
            case MalhaFechada.PI:
                txtKP.setEnabled(true);
                txtKD.setEnabled(false);
                txtTD.setEnabled(false);
                txtKI.setEnabled(true);
                txtTI.setEnabled(true);
                fireConfiguracaoStatusAtualizado();
                break;
            case MalhaFechada.PD:
                txtKP.setEnabled(true);
                txtKD.setEnabled(true);
                txtTD.setEnabled(true);
                txtKI.setEnabled(false);
                txtTI.setEnabled(false);
                fireConfiguracaoStatusAtualizado();
                break;
            case MalhaFechada.PID:
            case MalhaFechada.PI_D:
                txtKP.setEnabled(true);
                txtKD.setEnabled(true);
                txtTD.setEnabled(true);
                txtKI.setEnabled(true);
                txtTI.setEnabled(true);
                fireConfiguracaoStatusAtualizado();
                break;
        }
    }

    private void radioPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioPActionPerformed
        tipoControlador = MalhaFechada.P;
        ativarTxts(MalhaFechada.P);
    }//GEN-LAST:event_radioPActionPerformed

    private void radioPIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioPIActionPerformed
        tipoControlador = MalhaFechada.PI;
        ativarTxts(MalhaFechada.PI);
    }//GEN-LAST:event_radioPIActionPerformed

    private void radioPDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioPDActionPerformed
        tipoControlador = MalhaFechada.PD;
        ativarTxts(MalhaFechada.PD);
    }//GEN-LAST:event_radioPDActionPerformed

    private void radioPIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioPIDActionPerformed
        tipoControlador = MalhaFechada.PID;
        ativarTxts(MalhaFechada.PID);
    }//GEN-LAST:event_radioPIDActionPerformed

    private void radioPI_DActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioPI_DActionPerformed
        tipoControlador = MalhaFechada.PI_D;
        ativarTxts(MalhaFechada.PI_D);
    }//GEN-LAST:event_radioPI_DActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JRadioButton radioP;
    private javax.swing.JRadioButton radioPD;
    private javax.swing.JRadioButton radioPI;
    private javax.swing.JRadioButton radioPID;
    private javax.swing.JRadioButton radioPI_D;
    private javax.swing.JTextField txtKD;
    private javax.swing.JTextField txtKI;
    private javax.swing.JTextField txtKP;
    private javax.swing.JTextField txtTD;
    private javax.swing.JTextField txtTI;
    // End of variables declaration//GEN-END:variables
}
